package com.rfsp.common.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.rfsp.common.util.TreeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rfsp.common.base.BaseService;
import com.rfsp.common.shiro.ShiroUtils;
import com.rfsp.common.util.ValidUtil;
import com.rfsp.common.mapper.PrivilegeMapper;
import com.rfsp.common.mapper.RoleMapper;
import com.rfsp.common.mapper.UserMapper;
import com.rfsp.common.pojo.po.Privilege;
import com.rfsp.common.pojo.po.RolePO;
import com.rfsp.common.pojo.vo.OrgVO;
import com.rfsp.common.pojo.vo.PrivilegeVO;
import com.rfsp.common.pojo.vo.UserVO;

/**
 * 返回前台权限树状显示字符串
 * @author 李阳
 *
 */

@Service("priviService")
public class PriviService  extends BaseService<Privilege>{

	
	@Autowired
	private PrivilegeMapper privilegeMapper;
	
	@Autowired
	private RoleMapper roleMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	public List<PrivilegeVO> queryPrivilegeList(String id) {
		List<PrivilegeVO> voList = privilegeMapper.queryPrivilegeList(id);
		return voList;
	}	
	
	
	
	public List<PrivilegeVO> queryPrivilegeList4mcp(String roleCode) {
		List<PrivilegeVO> voList = privilegeMapper.queryPrivilegeList4mcp(roleCode);
		return voList;
	}
	
	
	public  List<PrivilegeVO> queryChildPriList(String roleCode) {
		List<PrivilegeVO> list = privilegeMapper.queryChildPriList(roleCode);
		return list;
	}
	
	
	public Integer queryChildPriListCount(String roleCode) {
		Integer count = privilegeMapper.queryChildPriListCount(roleCode);
		return count;
	}
	
	public void initRightByRoleCode(){
		
//		String[] roleCodes = {"104","141"};
//		String[] roleCodes = {"300","301"};
		//String[] roleCodes = {"2000","2001"};
		String[] roleCodes = {"mcp_bank","mcp_hos"};
		for(String code : roleCodes) {
			RolePO single = null;
			single = roleMapper.selectRoleByRoleCodes(code);
			if(single != null) {
				Set<String> url = getRightByRoleCode(code);
				ShiroUtils.addRolePerms(code, url);
			}
		}
	}
	
	public Set<String> getRightByRoleCode(String roleCode){
		Set<String> priSet = new LinkedHashSet<>();
		Set<String> priSetResult = new LinkedHashSet<>();
		priSet = privilegeMapper.getRightByRoleCode(roleCode);
		for(String single:priSet) {
			if(!ValidUtil.isEmpty(single)) {
				if(single.indexOf("?")>0) {
					single = single.substring(0,single.indexOf("?"));
					priSetResult.add(single);
				}else {
					priSetResult.add(single);
				}
			}else {
				continue;
			}

		}
		return priSetResult;
	}

	public List<Map<String,Object>> queryPrivList(String userCode,String superRightCode){
		return 	queryPrivilege(userCode,superRightCode);
	}

	public List<PrivilegeVO> queryPrivList2(String userCode,String superRightCode){
//		List<PrivilegeVO> childList = queryChildren(userCode, superRightCode);
		List<PrivilegeVO> permList = userMapper.getUserPriv2(userCode, "");
		return TreeUtil.makeTree(
				permList,
				p-> superRightCode.equals(p.getSupRightCode()),
				(p,c)->p.getRightCode().equals(c.getSupRightCode()),
				PrivilegeVO::setChildList);
	}
	
	private List<Map<String,Object>> queryPrivilege(String userCode,String superRightCode){
		List<PrivilegeVO> childList = queryChildren(userCode, superRightCode);
		List<Map<String,Object>> privilegeTree = new ArrayList<Map<String,Object>>();
		for (int i = 0; i < childList.size(); i++) {
			PrivilegeVO pl = childList.get(i);
			if(pl.getUrl()!=null && !"".equals(pl.getUrl())) {
				if(pl.getUrl().indexOf('?')>0) {
					if(pl.getUrl().indexOf("p_id=") == -1)
						{pl.setUrl(pl.getUrl()+"&p_id="+pl.getId());} 
				}else {
					if(pl.getUrl().indexOf("p_id=") == -1)
						{pl.setUrl(pl.getUrl()+"?p_id="+pl.getId());} 
				}
			}
			if(("4".equals(pl.getRightType()) || "3".equals(pl.getRightType()))){
				continue;
			}
			Map<String,Object> node = new HashMap<String, Object>();
			Map<String,Object> attribute = new HashMap<String,Object>();
			Map<String,Object> para = new HashMap<String,Object>();
			String url = pl.getUrl() == null ? "" : pl.getUrl();
			para.put("url",url);
			para.put("copyEnable",pl.getCopyEnable());
			para.put("childrenFunctions", queryPrivilegeByTypes(userCode, pl.getRightCode()));
			attribute.put("para", para);
			attribute.put("target", pl.getTarget());
			attribute.put("action", pl.getAction());
			node.put("attributes", attribute);
			node.put("priviId",pl.getId());
			node.put("id",pl.getRightCode());
			node.put("text",pl.getRightName());
			node.put("url", url);
			if(!"".equals(url))
			node.put("href","href=\""+url+"\"");
			List<Map<String,Object>> l = queryPrivilege(userCode, pl.getRightCode());
			if(null != l && l.size() > 0){
				node.put("children",l);
				node.put("navTreeList", l);
			}
			privilegeTree.add(node);
		}
		return privilegeTree;
	}
	
	
	
	public List<PrivilegeVO> queryChildren(String userCode,String superRightCode){
		List<PrivilegeVO> resultList = new ArrayList<PrivilegeVO>();
		Map<String ,Object > prarMap = new HashMap<String, Object>();
		prarMap.put("userCode", userCode);
		prarMap.put("superRightCode", superRightCode);
		List<PrivilegeVO> volist = userMapper.getUserPriv2(userCode, superRightCode);
//		if(volist!=null && volist.size()>0) {
//			for(PrivilegeVO aa: volist) {
//				if(ValidUtil.isEmpty(aa.getUrl())) {
////					aa.setUrl("");//顶级菜单没有url
//				}
//			}
//		}
		
		if(volist!=null && volist.size()>0) {
			for(PrivilegeVO aa: volist) {
				List<PrivilegeVO> aaa  = this.queryChildren(userCode, aa.getRightCode());
//				if(aaa!=null && aaa.size()>0) {
//					for(PrivilegeVO aaaa: aaa) {
//						if(ValidUtil.isEmpty(aaaa.getUrl())) {
////							aaaa.setUrl("/nullUrl");aa.setUrl("");//顶级菜单没有url
//						}
//					}
//				}
				aa.setChildList(aaa);
				resultList.add(aa);
			}
		}
		return resultList;
	}
	
	

	
	
	/**
	 * 根据类型找权限
	 * @param userCode
	 * @param superRightCode
	 * @return
	 */
	private List<Map<String,Object>> queryPrivilegeByTypes(String userCode,String superRightCode){
		List<PrivilegeVO> childList = queryChildren(userCode,superRightCode);
		List<Map<String,Object>> privilegeTree = new ArrayList<Map<String,Object>>();
		for (int i = 0; i < childList.size(); i++) {
			PrivilegeVO pl = childList.get(i);
			if(StringUtils.isNotEmpty(pl.getUrl())) {
				if(pl.getUrl().indexOf('?')>0) {
					if(pl.getUrl().indexOf("p_id=") == -1)
						{pl.setUrl(pl.getUrl()+"&p_id="+pl.getId());} 
				}else {
					if(pl.getUrl().indexOf("p_id=") == -1)
						{pl.setUrl(pl.getUrl()+"?p_id="+pl.getId());} 
				}
			}
			
			boolean pushFlag = "3".equals(pl.getRightType())||"4".equals(pl.getRightType());
			
			if(pushFlag){
				Map<String,Object> para = new HashMap<String,Object>();
				Map<String,Object> attribute = new HashMap<String,Object>();
				Map<String,Object> node = new HashMap<String, Object>();

				para.put("url", pl.getUrl());
				para.put("copyEnable", pl.getCopyEnable());
				para.put("childrenFunctions", queryPrivilegeByTypes(userCode, pl.getRightCode()));

				attribute.put("para", para);
				attribute.put("target", pl.getTarget());
				attribute.put("action", pl.getAction());

				node.put("attributes", attribute);
				node.put("priviId", pl.getId());
				node.put("id", pl.getRightCode());
				node.put("text", pl.getRightName());
				node.put("url", pl.getUrl());
				
				List<Map<String,Object>> l = queryPrivilege(userCode, pl.getRightCode());
				if(null != l && l.size() > 0){
					node.put("children",l);
					node.put("navTreeList", l);
				}
				privilegeTree.add(node);
			}
		}
		
		return privilegeTree;
	}
	
	

	
	public List<String> queryRoleCodeList(UserVO userVO, OrgVO orgVO) {
		
		List<String> voList =  jdbc.queryForList("select * from m_user_org", String.class);
		return voList;
	}
	
	
	
	
	
	
	
	
}
